//************************************************************************
//Developed by Guo Ming Li 2009/2010
//Source code:
//  The source code may be freely modified or used but this credit section may not be removed 
//  The original author must be notified of any modifications plus a copy of 
//  the changed source code
//Contact info: mailto://gming.li@gmail.com
//************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;

namespace GML.BizTalk.OperationManager.MSBTSOperations
{
    public partial class Orchestration: IComparable
    {
        public static Microsoft.BizTalk.ExplorerOM.BtsAssembly FindAssemblyByFullName
            (BtsAssemblyCollection assemblies, string fullName)
        {
            foreach (Microsoft.BizTalk.ExplorerOM.BtsAssembly assembly in assemblies)
            {
                if (assembly.DisplayName == fullName)
                    return assembly;
            }
            return null;
        }

        private BtsCatalogExplorer catalog = null;
        public int CompareTo(object obj)
        {
            if (obj is Orchestration)
            {
                Orchestration orchObj = ((Orchestration)obj);
                if (catalog == null)
                {
                    catalog = new BtsCatalogExplorer();
                    catalog.ConnectionString = GML.BizTalk.OperationManager.Library.Helper.GetConnectionString(this.BtsDbServerName, this.BtsManagmentDbName);
                }
                try
                {
                    //first comparison
                    string orchestrationAssembly = this.BtsAssembly.DisplayName;
                    string orchestrationName = this.Fullname;
                    Microsoft.BizTalk.ExplorerOM.BtsAssembly assembly = FindAssemblyByFullName(catalog.Assemblies, orchestrationAssembly);
                    if (assembly == null)
                    {
                        System.Diagnostics.Debug.WriteLine(
                                "Compare=0 for: " +
                                this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                        );
                        return 0;
                    }

                    Microsoft.BizTalk.ExplorerOM.BtsOrchestration orchestration = assembly.Orchestrations[orchestrationName];
                    if (orchestration == null)
                    {
                        System.Diagnostics.Debug.WriteLine(
                                "Compare=0 for: " +
                                this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                        );
                        return 0;
                    }

                    BtsOrchestrationCollection InvokedOrchestrations = orchestration.InvokedOrchestrations;
                    if (InvokedOrchestrations == null)
                    {
                        System.Diagnostics.Debug.WriteLine(
                                "Compare=0 for: " +
                                this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                        );
                        return 0;
                    }

                    foreach (Microsoft.BizTalk.ExplorerOM.BtsOrchestration InvokedOrchestration in InvokedOrchestrations)
                    {
                        if (InvokedOrchestration.FullName.Equals(orchObj.Fullname) && InvokedOrchestration.Application.Name.Equals(orchObj.ApplicationName) &&
                            InvokedOrchestration.BtsAssembly.DisplayName.Equals(orchObj.BtsAssembly.DisplayName))
                        {
                            System.Diagnostics.Debug.WriteLine(
                                    "Compare=" + (-InvokedOrchestrations.Count) + " for: " +
                                    this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                    orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                            );
                            return -InvokedOrchestrations.Count;
                        }
                    }
                    //second comparison
                    orchestrationAssembly = orchObj.BtsAssembly.DisplayName;
                    orchestrationName = orchObj.Fullname;
                    assembly = FindAssemblyByFullName(catalog.Assemblies, orchestrationAssembly);
                    if (assembly == null)
                    {
                        System.Diagnostics.Debug.WriteLine(
                                "Compare=0 for: " +
                                this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                        );
                        return 0;
                    }

                    orchestration = assembly.Orchestrations[orchestrationName];
                    if (orchestration == null)
                    {
                        System.Diagnostics.Debug.WriteLine(
                                "Compare=0 for: " +
                                this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                        );
                        return 0;
                    }

                    InvokedOrchestrations = orchestration.InvokedOrchestrations;
                    if (InvokedOrchestrations == null)
                    {
                        System.Diagnostics.Debug.WriteLine(
                                "Compare=0 for: " +
                                this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                        );
                        return 0;
                    }

                    foreach (Microsoft.BizTalk.ExplorerOM.BtsOrchestration InvokedOrchestration in InvokedOrchestrations)
                    {
                        if (InvokedOrchestration.FullName.Equals(this.Fullname) && InvokedOrchestration.Application.Name.Equals(this.ApplicationName) &&
                            InvokedOrchestration.BtsAssembly.DisplayName.Equals(this.BtsAssembly.DisplayName))
                        {
                            System.Diagnostics.Debug.WriteLine(
                                    "Compare=" + (InvokedOrchestrations.Count) + " for: " +
                                    this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                    orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                            );
                            return InvokedOrchestrations.Count;
                        }
                    }
                        System.Diagnostics.Debug.WriteLine(
                                "Compare=0 for: " +
                                this.Fullname + ", " + this.BtsAssembly.DisplayName + " ::: " +
                                orchObj.Fullname + ", " + orchObj.BtsAssembly.DisplayName
                            );
                        return 0;
                }
                catch (Exception e)
                {
                    catalog.DiscardChanges();
                    throw e;
                }

            }
            else
            {
                throw new ArgumentException("Object is not a Orchestration");
            }
        }

        public int GetInvokedOrchestrations()
        {
                if (catalog == null)
                {
                    catalog = new BtsCatalogExplorer();
                    catalog.ConnectionString = GML.BizTalk.OperationManager.Library.Helper.GetConnectionString(this.BtsDbServerName, this.BtsManagmentDbName);
                }
                try
                {
                    //first comparison
                    string orchestrationAssembly = this.BtsAssembly.DisplayName;
                    string orchestrationName = this.Fullname;
                    Microsoft.BizTalk.ExplorerOM.BtsAssembly assembly = FindAssemblyByFullName(catalog.Assemblies, orchestrationAssembly);
                    if (assembly == null)
                    {
                        return 0;
                    }

                    Microsoft.BizTalk.ExplorerOM.BtsOrchestration orchestration = assembly.Orchestrations[orchestrationName];
                    if (orchestration == null)
                    {
                        return 0;
                    }

                    BtsOrchestrationCollection InvokedOrchestrations = orchestration.InvokedOrchestrations;
                    if (InvokedOrchestrations == null)
                    {
                        return 0;
                    }

                    foreach (Microsoft.BizTalk.ExplorerOM.BtsOrchestration InvokedOrchestration in InvokedOrchestrations)
                    {
                        System.Diagnostics.Debug.WriteLine("\tInvokeOrch: \t" +
                                InvokedOrchestration.FullName + ", " +
                                InvokedOrchestration.BtsAssembly.DisplayName + ", ApplicationName = " + InvokedOrchestration.Application.Name);
                    }
                    return 0;
                }
                catch (Exception e)
                {
                    catalog.DiscardChanges();
                    throw e;
                }

        }

    }
}
